using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace Accelerometer
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// Accel object instance
        /// </summary>
        Accel accel = new Accel();

        IntPtr handle = IntPtr.Zero;

        public Form1()
        {
            InitializeComponent();
            this.Text = this.Text + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            switch (accel.gsel)
            {
                case Accel.GSEL.R2G:
                    this.rb2G.Checked = true;
                    break;
                case Accel.GSEL.R4G:
                    this.rb4G.Checked = true;
                    break;
                case Accel.GSEL.R8G:
                    this.rb8G.Checked = true;
                    break;
            }
        }

        private void updatePanels()
        {
            tbX.Value = 0;
            tbY.Value = 0;
            tbZ.Value = 0;
            this.pnlGSlider.Enabled = true;
            switch (accel.gsel)
            {
                case Accel.GSEL.R2G:
                    lblGRange.Text = "+/- 2G";
                    tbX.Minimum = -2300;
                    tbX.Maximum = 2300;
                    tbY.Minimum = -2300;
                    tbY.Maximum = 2300;
                    tbZ.Minimum = -2300;
                    tbZ.Maximum = 2300;
                    break;
                case Accel.GSEL.R4G:
                    lblGRange.Text = "+/- 4G";
                    tbX.Minimum = -4300;
                    tbX.Maximum = 4300;
                    tbY.Minimum = -4300;
                    tbY.Maximum = 4300;
                    tbZ.Minimum = -4300;
                    tbZ.Maximum = 4300;
                    break;
                case Accel.GSEL.R8G:
                    lblGRange.Text = "+/- 8G";
                    tbX.Minimum = -8300;
                    tbX.Maximum = 8300;
                    tbY.Minimum = -8300;
                    tbY.Maximum = 8300;
                    tbZ.Minimum = -8300;
                    tbZ.Maximum = 8300;
                    break;
            }
        }

        private void tmrUpdate_Tick(object sender, EventArgs e)
        {
            Accel.XYZGForce gforce = new Accel.XYZGForce();
            gforce = accel.gForce;
            tbX.Value = gforce.X;
            tbY.Value = gforce.Y;
            tbZ.Value = gforce.Z;
            lblXAxisValue.Text = Convert.ToString(gforce.X);
            lblYAxisValue.Text = Convert.ToString(gforce.Y);
            lblZAxisValue.Text = Convert.ToString(gforce.Z);

        }

        private void rb2G_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rb2G.Checked)
            {
                accel.gsel = Accel.GSEL.R2G;
                updatePanels();
            }
        }

        private void rb4G_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rb4G.Checked)
            {
                accel.gsel = Accel.GSEL.R4G;
                updatePanels();
            }

        }

        private void rb8G_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rb8G.Checked)
            {
                accel.gsel = Accel.GSEL.R8G;
                updatePanels();
            }
        }

   }
}